from django.test import TestCase


import pprint
import json
import random
import requests
import re
from pypinyin import pinyin, Style

class Spider():
    def __init__(self):

        self.headers = {
                "authority": "mapi.vip.com",
                "accept": "*/*",
                "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                "cache-control": "no-cache",
                "pragma": "no-cache",
                "referer": "https://category.vip.com/",
                "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Microsoft Edge\";v=\"122\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"Windows\"",
                "sec-fetch-dest": "script",
                "sec-fetch-mode": "no-cors",
                "sec-fetch-site": "same-site",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"
            }
        self.cookies = {
                "VipDFT": "1",
                "mars_cid": "1708282140502_e3538ad28fd84440e2a0c5c7934fa8cb",
                "vip_first_visitor": "1",
                "vip_address": "%257B%2522pid%2522%253A%2522103104%2522%252C%2522cid%2522%253A%2522103104105%2522%252C%2522pname%2522%253A%2522%255Cu5b89%255Cu5fbd%255Cu7701%2522%252C%2522cname%2522%253A%2522%255Cu9a6c%255Cu978d%255Cu5c71%255Cu5e02%2522%257D",
                "vip_province": "103104",
                "vip_province_name": "%E5%AE%89%E5%BE%BD%E7%9C%81",
                "vip_city_name": "%E9%A9%AC%E9%9E%8D%E5%B1%B1%E5%B8%82",
                "vip_city_code": "103104105",
                "vip_wh": "VIP_HZ",
                "vip_ipver": "31",
                "user_class": "a",
                "VipUINFO": "luc%3Aa%7Csuc%3Aa%7Cbct%3Ac_new%7Chct%3Ac_new%7Cbdts%3A0%7Cbcts%3A0%7Ckfts%3A0%7Cc10%3A0%7Crcabt%3A0%7Cp2%3A0%7Cp3%3A0%7Cp4%3A0%7Cp5%3A1%7Cul%3A3105",
                "vpc_uinfo": "fr1352%3A0%2Cfr674%3AD1%2Cfr766%3A0%2Cfr896%3A0%2Cfr392%3A310505%2Cfr398%3A0%2Cfr408%3A0%2Cfr251%3AA%2Cfr1195%3A0%2Cfr848%3A0%2Cfr1196%3A0%2Cfr902%3A0%2Cfr901%3A0%2Cfr980%3A0%2Cfr1570%3A0%2Cfr713%3A0%2Cfr1575%3A0%2Cfr1051%3A0%2Cfr259%3AS0-4%2Cfr1655%3A0%2Cfr884%3A0%2Cfr863%3A0%2Cfr1527%3A0%2Cfr344%3A0%2Cfr249%3AA1%2Cfr328%3A3105%2Cfr1544%3A0%2Cfr1543%3A0%2Cfr1521%3A0",
                "mst_area_code": "104104",
                "mars_sid": "7a82bf0c7f450720de72e1f84101eb3d",
                "visit_id": "DEBBACD09723B4F080630FD1B99C700F",
                "VIP_QR_FIRST": "1",
                "mars_pid": "35",
                "vip_access_times": "%7B%22detail%22%3A4%2C%22list%22%3A5%7D",
                "pg_session_no": "11",
                "vip_tracker_source_from": ""
            }
        self.url = "https://mapi.vip.com/vips-mobile/rest/shopping/pc/detail/main/v6"





    def main(self):
        for page in range(0, 2400, 120):
            import requests

            headers = {
                "authority": "mapi.vip.com",
                "accept": "*/*",
                "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
                "cache-control": "no-cache",
                "pragma": "no-cache",
                "referer": "https://category.vip.com/",
                "sec-ch-ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Microsoft Edge\";v=\"122\"",
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": "\"Windows\"",
                "sec-fetch-dest": "script",
                "sec-fetch-mode": "no-cors",
                "sec-fetch-site": "same-site",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"
            }
            cookies = {
                "VipDFT": "1",
                "mars_cid": "1708282140502_e3538ad28fd84440e2a0c5c7934fa8cb",
                "vip_first_visitor": "1",
                "vip_address": "%257B%2522pid%2522%253A%2522103104%2522%252C%2522cid%2522%253A%2522103104105%2522%252C%2522pname%2522%253A%2522%255Cu5b89%255Cu5fbd%255Cu7701%2522%252C%2522cname%2522%253A%2522%255Cu9a6c%255Cu978d%255Cu5c71%255Cu5e02%2522%257D",
                "vip_province": "103104",
                "vip_province_name": "%E5%AE%89%E5%BE%BD%E7%9C%81",
                "vip_city_name": "%E9%A9%AC%E9%9E%8D%E5%B1%B1%E5%B8%82",
                "vip_city_code": "103104105",
                "vip_wh": "VIP_HZ",
                "vip_ipver": "31",
                "user_class": "a",
                "VipUINFO": "luc%3Aa%7Csuc%3Aa%7Cbct%3Ac_new%7Chct%3Ac_new%7Cbdts%3A0%7Cbcts%3A0%7Ckfts%3A0%7Cc10%3A0%7Crcabt%3A0%7Cp2%3A0%7Cp3%3A0%7Cp4%3A0%7Cp5%3A1%7Cul%3A3105",
                "vpc_uinfo": "fr1352%3A0%2Cfr674%3AD1%2Cfr766%3A0%2Cfr896%3A0%2Cfr392%3A310505%2Cfr398%3A0%2Cfr408%3A0%2Cfr251%3AA%2Cfr1195%3A0%2Cfr848%3A0%2Cfr1196%3A0%2Cfr902%3A0%2Cfr901%3A0%2Cfr980%3A0%2Cfr1570%3A0%2Cfr713%3A0%2Cfr1575%3A0%2Cfr1051%3A0%2Cfr259%3AS0-4%2Cfr1655%3A0%2Cfr884%3A0%2Cfr863%3A0%2Cfr1527%3A0%2Cfr344%3A0%2Cfr249%3AA1%2Cfr328%3A3105%2Cfr1544%3A0%2Cfr1543%3A0%2Cfr1521%3A0",
                "mst_area_code": "104104",
                "mars_sid": "7a82bf0c7f450720de72e1f84101eb3d",
                "visit_id": "DEBBACD09723B4F080630FD1B99C700F",
                "VIP_QR_FIRST": "1",
                "mars_pid": "35",
                "vip_access_times": "%7B%22detail%22%3A4%2C%22list%22%3A5%7D",
                "pg_session_no": "11",
                "vip_tracker_source_from": ""
            }
            url = "https://mapi.vip.com/vips-mobile/rest/shopping/pc/search/product/rank"
            params = {
                "callback": "getMerchandiseIds",
                "app_name": "shop_pc",
                "app_version": "4.0",
                "warehouse": "VIP_HZ",
                "fdc_area_id": "103104105",
                "client": "pc",
                "mobile_platform": "1",
                "province_id": "103104",
                "api_key": "70f71280d5d547b2a7bb370a529aeea1",
                "user_id": "",
                "mars_cid": "1708282140502_e3538ad28fd84440e2a0c5c7934fa8cb",
                "wap_consumer": "a",
                "standby_id": "nature",
                "keyword": "手机",
                "lv3CatIds": "",
                "lv2CatIds": "",
                "lv1CatIds": "",
                "brandStoreSns": "",
                "props": "",
                "priceMin": "",
                "priceMax": "",
                "vipService": "",
                "sort": "0",
                "pageOffset": "0",
                "channelId": "1",
                "gPlatform": "PC",
                "batchSize": "120",
                "_": "1716219865667"
            }
            response = requests.get(url, headers=headers, cookies=cookies, params=params).text




            # 获得产品编码
            pid_code = re.findall('{"pid":"(.*?)"}', response)

            for pid in pid_code:

                self.spider_data(pid)





    def brand(self,response):
        # 品牌
        brandStoreInfo = response['data']['brandStoreInfo']['brandStoreName']
        # Logo
        brandStoreLogo = response['data']['brandStoreInfo']['brandStoreLogo']
        # 首字母
        if brandStoreInfo:
            first_char_pinyin_list = pinyin(brandStoreInfo[0], style=Style.FIRST_LETTER, strict=False)
            # 取第一个拼音的第一个字母（转换为大写）
            first_letter =  first_char_pinyin_list[0][0].upper()
        else:
            first_letter = None
        print(first_letter)



    def spu(self,response):
        # spu
        spu_name = response['data']['base']['title']
        spu_sales = 0
        spu_comments = 0


        url = "https://mapi.vip.com/vips-mobile/rest/shopping/pc/detail/vendorqa/v1"
        params = {
            "callback": "getVendorQaCb",
            "app_name": "shop_pc",
            "app_version": "4.0",
            "warehouse": "VIP_HZ",
            "fdc_area_id": "103104105",
            "client": "pc",
            "mobile_platform": "1",
            "province_id": "103104",
            "api_key": "70f71280d5d547b2a7bb370a529aeea1",
            "user_id": "",
            "mars_cid": "1703946163504_4eebec221de3364e0da3bbe4a2182454",
            "wap_consumer": "a",
            "productId": "6920549355391848472"
        }
        response_pack = requests.get(url, headers=self.headers, cookies=self.cookies, params=params).text.replace('getVendorQaCb(','').replace(')','')
        response_pack_dict = json.loads(response_pack)
        decs_pack = response_pack_dict['data']['list']


        # 包装清单
        decs_data = ''
        for decs in decs_pack:
            decs_data += f"""
            <dl class="q-tit-item">
                    <dt class="qti-title">
                        <i class="qti-question"></i>
                        <p class="qti-txt" style="color: red">{decs['question']}</p>
                    </dt>
                    <dd class="qti-content">
                        <i class="qti-answer"></i>
                        <p class="qti-txt">
                            {decs['answer']}
                        </p>
                    </dd>
                </dl>
            """


        # 售后服务
        tsaleService_data = ''
        tsaleServiceListitle = response['data']['saleServiceList']
        for tsaleService in tsaleServiceListitle:
            tsaleService_data += f"""
                <dl class="q-tit-item">
                        <dt class="qti-title">
                            <i class="qti-question"></i>
                            <p class="qti-txt" style="color: red">{tsaleService['name']}</p>
                        </dt>
                        <dd class="qti-content">
                            <i class="qti-answer"></i>
                            <p class="qti-txt">
                                {tsaleService['value']}
                            </p>
                        </dd>
                    </dl>
                """

    def sku(self, response):
        # sku

        # 名称
        title = response['data']['base']['title']

        saleProps_list = []
        products = response['data']['products'].values()

        for product in products:
            # 标题
            longTitle = product['merchandiseSn']
            # 名称
            merchandiseSn = product['merchandiseSn']
            # 市场价
            saleMarketPrice = product['priceView']['salePrice']['saleMarketPrice']
            # 售价
            salePrice = product['priceView']['salePrice']['salePrice']
            # 折扣
            cost_price = int(saleMarketPrice) - int(salePrice)
            # 库存
            stock = 1000
            # 销量
            sales = random.randint(1, 800)
            # 评价
            comments = 0
            # 是否上架
            is_launched = True
            # 默认图片
            default_image = product['smallImage']
            # 商品ID
            productId = product['productId']
            saleProps_dict = {
                'name': merchandiseSn,
                'price': salePrice,
                'caption': title,
                'cost_price': cost_price,
                'market_price': saleMarketPrice,
                'stock': stock,
                'sales': sales,
                'comments': comments,
                'is_launched': is_launched,
                'default_image': default_image,
                'productId' : productId
            }

            saleProps_list.append(saleProps_dict)
        return saleProps_list



    def sku_image(self,response):
        images_detls = response['data']['images']['groups'].values()
        SKUImage_detail = []
        # 商品详情图
        for detil in images_detls:
            image_lsit = detil['previewImages']
            for image_urls in image_lsit:
                image_url = image_urls['imageUrl'].replace('http://h2.appsimg.com/a.appsimg.com/upload/merchandise/','').replace('.jpg','')
                SKUImage_detail.append(image_url)
            break

        print(SKUImage_detail)


    def spu_pecification(self,response):
        # 规格
        saleProps_list = []
        saleProps = response['data']['saleProps']
        for saleProp in saleProps:
            name = saleProp['name']
            saleProps_list.append(name)
        print(saleProps_list)

    def spu_optiuons(self, response):
        saleProps = response['data']['saleProps']
        sub_dict = {}
        type_list = []
        for index, saleProp in enumerate(saleProps):
            type_name = saleProp['name']
            type_list.append(type_name)
            data_list = []
            for sub_saleProp in saleProp.get('values'):
                detail_name = sub_saleProp['name']
                # 如果第一级素具不存在productId则productId按第二级数据取
                if sub_saleProp.get('productId') is None:
                    productId = ''
                else:
                    productId = sub_saleProp['productId']

                data_dict = {
                    'productId': productId,
                    'detail_name': detail_name,
                }
                data_list.append(data_dict)

            if type_name not in sub_dict:
                sub_dict[type_name] = data_list
        print(sub_dict)
        return  sub_dict






    # 获得所有商品规格信息
    def good_type(self , response):
        tatil_dict = {}
        saleProps = response['data']['saleProps']
        for saleProp in saleProps:
            pid = saleProp['pid']
            name = saleProp['name']
            type = saleProp['type']
            sale_dict = {
                'name': name,
                'pid': pid,
                'type': type,
            }
            value_list = []
            for value in saleProp['values']:
                name = value['name']
                vid = value['vid']
                value_dict = {
                    'name': name,
                    'vid': vid,
                }
                sale_dict[vid] = name
                value_list.append(value_dict)
            sale_dict['values'] = value_list
            tatil_dict[pid] = sale_dict
        pprint.pprint(tatil_dict)
        return tatil_dict


    # 拿到所有的商品规格
    def get_type(self , response):
        tatil_dict = self.good_type(response)
        # pprint.pprint(tatil_dict)

        skus = response['data']['skus']
        sku_keys = skus.keys()
        sku_values = skus.values()
        productId_dict = {}
        for key , value in zip(sku_keys,sku_values):
            productId = value['productId']
            ids = key.split(';')
            id_list = []
            for id in ids:
                pid = id.split(':')[0]
                vid = id.split(':')[1]

                id_dict = {
                    'pid':pid,
                    'vid':vid,
                }
                id_list.append(id_dict)
            productId_dict[productId] = id_list
        # print(productId_dict)


        saleProps_list = self.sku(response)

        # 获取到该商品的对应信息
        for sale in saleProps_list:
            productId = sale['productId']
            params = productId_dict[productId]
            for param in params:
                pid = param['pid']
                vid = param['vid']

                param_dict = {
                    'name' : tatil_dict[pid]['name'],
                    'type' : tatil_dict[pid][vid]
                }
                # print(param_dict)














    def spider_data(self,pid):

        self.data = {
    "app_name": "shop_pc",
    "app_version": "4.0",
    "warehouse": "VIP_HZ",
    "fdc_area_id": "103104105",
    "client": "pc",
    "mobile_platform": "1",
    "province_id": "103104",
    "api_key": "70f71280d5d547b2a7bb370a529aeea1",
    "user_id": "",
    "mars_cid": "1708282140502_e3538ad28fd84440e2a0c5c7934fa8cb",
    "wap_consumer": "a",
    "scene": "detail",
    "productId": f"{pid}",
    "opts": "priceView:13;quotaInfo:1;restrictTips:1;panelView:3;foreShowActive:1;invisible:1;floatingView:1;announcement:1;svipView:2;showSingleColor:1;svipPriceMode:1;promotionTips:6;foldTips:3;formula:2;extraDetailImages:1;shortVideo:1;countryFlagStyle:1;saleServiceList:1;storeInfo:2;brandCountry:1;freightTips:3;priceBannerView:1;bannerTagsView:1;buyMoreFormula:1;mergeGiftTips:0;kf:1;priceIcon:1;tuv:3;promotionTags:7;mergeGiftTips:3;topDetailImage:2;deliveryInfo:1;relatedProdSpu:1"
}
        response = requests.post(self.url, headers=self.headers, cookies=self.cookies, data=self.data).json()


        self.get_type(response)



if __name__ == '__main__':
    spider = Spider()
    spider.main()
